package com.bdqn.BookManage.servlet;

import com.bdqn.BookManage.view.BookInfoView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @author : CP
 * @version: 1.0
 * @program : BookManage
 * @description : 是否选中多选框处理器
 * @date : 2020-07-29 12:19
 **/
@WebServlet("/isSelect")
public class IsSelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();

        /** 获取数据 */
        String id = req.getParameter("id");
        Integer state = Integer.valueOf(req.getParameter("state"));

        /** 从购物车提取数据 */
        Map<String, BookInfoView> bookInfoViewMap = (Map<String, BookInfoView>) session.getAttribute("cart");
        BookInfoView bookInfoView = bookInfoViewMap.get(id);

        bookInfoView.setIsSelected(state);

        out.println(1);
        out.flush();
        out.close();
    }
}
